/**
 * admin 后台js
 *
 */
//字符串为空
function isNull(str) {
    if (str == null || str == "" || str.length < 1)
        return true;
    else
        return false;
}

/*检查输入框是否为空，并输出错误提示*/
function checkEmpty(data, holder, msg) {
    if (isNull(data)) {
        $(holder).html(msg);
        return false;
    } else {
        $(holder).html('');
        return true;
    }
}

//获取textarea字符长度，中文单字2字符，英文1字符
function getblen(str) {
    var l = str.length;
    var blen = 0;

    for (i = 0; i < l; i++) {
        if ((str.charCodeAt(i) & 0xff00) != 0) {
            blen++;
        }
        blen++;
    }

    return blen;
}

//textarea 字数提示
function textarealength(obj, toplength) {
    var note = $(obj).val();
    var blen = getblen(note);

    if (blen <= toplength) {
        $(".textarea-length").html(blen);
    } else {
        $(".textarea-length").css('color', 'red');
        $(".textarea-length").html(blen);
    }
}

/* 删除列表条目 */
function del(obj, id) {
    layer.confirm('确认要删除吗？', function (index) {
        $.post('del', {
            'id': id
        }, function (data) {
            if (data.code == 2) {
                $(obj).parents("tr").remove();
                layer.msg(data.msg, {
                    icon: 1,
                    time: 1000
                });
            } else {
                layer.msg(data.msg, {
                    icon: 5,
                    time: 2000
                });
                return false;
            }
        }, 'json');
    });
}

/* 删除列表多选条目 */
function del_all() {
    var checkbox = $('.text-c input[name="checkbox[]"]');
    var ids = new Array();
    checkbox.each(function (x) {
        if (this.checked)
            ids.push(this.value);
    })

    var length = ids.length;

    if (length == 0) {
        layer.msg('请选择要删除的选项', {
            icon: 5,
            time: 1000
        });
        return false;
    }

    layer.confirm('确认要删除吗？', function (index) {
        $.post('del', {
            'id': ids
        }, function (data) {
            if (data.code == 2) {
                checkbox.each(function (x) {
                    if (this.checked)
                        $(this).parents("tr").remove();
                })
                layer.msg(data.msg, {
                    icon: 1,
                    time: 1000
                });
            } else {
                layer.msg(data.msg, {
                    icon: 5,
                    time: 2000
                });
                return false;
            }
        }, 'json');
    });
}

/* ajax 改变 status 值 */
function status(action, obj, id) {
    var value = 2;
    var title = '启用';
    var status = '停用';
    var color = 'danger';
    if (action == 'start') {
        value = 1;
        action = 'stop';
        title = '停用';
        status = '启用';
        color = 'success';
    } else {
        action = 'start';
    }

    var text = "<a href=\"javascript:void(0);\" title='" + title + "' onClick=\"status('" + action + "',this," + id + ")\">";
    text += '<span class="label label-' + color + ' radius" >' + status + '</span>';
    text += '</a>';

    $.post('status', {
        'id': id,
        'value': value
    }, function (data) {
        if (data.code == 2) {
            $(obj).after(text);
            $(obj).remove();
            layer.msg(data.msg, {
                icon: 6,
                time: 1000
            });
        } else {
            layer.msg(data.msg, {
                icon: 5,
                time: 1000
            });
        }
    }, 'json');
}